package com.air.antispider.stream.dataprocess.businessprocess

import com.air.antispider.stream.common.bean.RequestType
import com.air.antispider.stream.dataprocess.constants.{BehaviorTypeEnum, FlightTypeEnum}
import com.air.antispider.stream.dataprocess.constants.FlightTypeEnum.FlightTypeEnum

import scala.collection.mutable.ArrayBuffer

/**
  * 对请求类型进行打标签
  */
object RequestTypeClassifier {

  def classifyByRequest(url: String, classifyRuleMap: Map[String, ArrayBuffer[String]]): RequestType = {
      //先将Map中的4个规则列表取出来
//    val nationalQueryListOption: Option[ArrayBuffer[String]] = classifyRuleMap.get("nationalQueryList")
//    nationalQueryListOption match {
//      case Some(v) =>
//      case None =>
//    }
    //从map中取出国内查询,如果取不到,返回null
    val nationalQueryList: ArrayBuffer[String] = classifyRuleMap.getOrElse("nationalQueryList", null)
    if (nationalQueryList != null) {
      //如果能取到数据,那么开始遍历
      for (rule <- nationalQueryList) {
        if (url.matches(rule)) {
          //如果能匹配上,代表属于该规则
          return RequestType(FlightTypeEnum.National, BehaviorTypeEnum.Query)
        }
      }
    }
    //如果国内查询没有匹配上,那么看看其它规则能不能匹配上
    val nationalBookList: ArrayBuffer[String] = classifyRuleMap.getOrElse("nationalBookList", null)
    if (nationalBookList != null) {
      //如果能取到数据,那么开始遍历
      for (rule <- nationalBookList) {
        if (url.matches(rule)) {
          //如果能匹配上,代表属于该规则
          return RequestType(FlightTypeEnum.National, BehaviorTypeEnum.Book)
        }
      }
    }
    val internationalQueryList: ArrayBuffer[String] = classifyRuleMap.getOrElse("internationalQueryList", null)
    if (internationalQueryList != null) {
      //如果能取到数据,那么开始遍历
      for (rule <- internationalQueryList) {
        if (url.matches(rule)) {
          //如果能匹配上,代表属于该规则
          return RequestType(FlightTypeEnum.International, BehaviorTypeEnum.Query)
        }
      }
    }

    val internationalBookList: ArrayBuffer[String] = classifyRuleMap.getOrElse("internationalBookList", null)
    if (internationalBookList != null) {
      //如果能取到数据,那么开始遍历
      for (rule <- internationalBookList) {
        if (url.matches(rule)) {
          //如果能匹配上,代表属于该规则
          return RequestType(FlightTypeEnum.International, BehaviorTypeEnum.Book)
        }
      }
    }
    //如果代码走到这里,还没有返回,代表所有的规则都没匹配上
    RequestType(FlightTypeEnum.Other, BehaviorTypeEnum.Other)
  }

}
